<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Route;
use think\Session;

class Person extends Controller
{
    public function shujia(Request $request)
    {
        $p=$request->post();
        $uid=Session::get('id','think');
//        return json($uid);
        if ($uid) {
            $list = db('personshu')->where('uid', $uid)->where('sid', $p['id'])->find();
            if ($list){
                $info=['stat'=>4];
            }else{
                $data=[
                    'uid'=>$uid,
                    'sid'=>$p['id']
                ];
                $res=db('personshu')->insert($data);
                if ($res){
                    $info=['stat'=>1];
                }else{
                    $info=['stat'=>2];
                }
            }
            return json($info);
//

        }else{
            $info = ['stat'=>3];
            return json($info);
        }
    }


    public function showshu()
    {
        $uid=Session::get('id');
        if ($uid){
            $res4=db('category')->where('pid',0)->field('id,cname')->select();
            $data=Db::table('lamp_personshu ps')
                ->where('uid',$uid)
                ->join('lamp_txt t','t.id=ps.sid')
                ->select();
            return view('person/personshu',['data4'=>$res4,'data'=>$data]);
        }else{
            return $this->error('请先登录');
        }

//        dump($data);die;

    }


    public function delshu($id)
    {
//        return json($id);
        $uid=Session::get('id');
//        return json($p);
        $data=db('personshu')
            ->where('sid',$id)
            ->where('uid',$uid)
            ->find();
        $res=db('personshu')->where('id',$data['id'])->delete();
        if ($res){
            $info = [
                'stat' => 1
            ];
        }else{
            $info = [
                'stat' => 2
            ];
        }
        return json($info);
    }
}
